﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace VirtualControl
{
    public class VirtualDateAxis : VirtualBase
    {
        public event EventHandler YearChanged;
        public event EventHandler MonthChanged;
        public event EventHandler DayChanged;

        protected void OnYearChanged(EventArgs e)
        {
            this.Months.Clear();
            this.Month = 0;
            this.Days.Clear();
            this.Day = 0;
            if (this.YearChanged != null) this.YearChanged(this, e);
            this.Invalidate();
        }

        protected void OnMonthChanged(EventArgs e)
        {
            this.Days.Clear();
            this.Day = 0;
            if (this.MonthChanged != null) this.MonthChanged(this, e);
            this.Invalidate();
        }

        protected void OnDayChanged(EventArgs e)
        {
            if (this.DayChanged != null) this.DayChanged(this, e);
            this.Invalidate();
        }

        public readonly List<int> Years = new List<int>();
        public readonly List<int> Months = new List<int>();
        public readonly List<int> Days = new List<int>();

        public int Year { get; private set; }
        public int Day { get; private set; }
        public int Month { get; private set; }

        public DateTime Value { get { return new DateTime(Year, Month, Day); } }

        Rectangle[] rects = new Rectangle[9];

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Rectangle r = this.Bounds;
            for (int i = 0; i < 9; i+=3)
            {
                rects[i] = new Rectangle(r.Left + r.Width * i / 9, r.Top, r.Height, r.Height);
                rects[i + 1] = new Rectangle(r.Left + r.Width * i / 9 + r.Height, r.Top, r.Width / 3 - r.Height * 2, r.Height);
                rects[i + 2] = new Rectangle(r.Left + r.Width * (i + 3) / 9 - r.Height, r.Top, r.Height, r.Height);
            }
        }

        public VirtualDateAxis(Control parent) : base(parent) { }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            int index = e.X * 9 / this.Bounds.Width;
            int indexYear = 0;
            int indexMonth = 0;
            int indexDay = 0;
            switch (index)
            {
                case 0:
                    indexYear = this.Years.IndexOf(this.Year);
                    if (indexYear > 0 && indexYear < this.Years.Count)
                    {
                        this.Year = this.Years[indexYear - 1];
                        OnYearChanged(null);
                    }
                    break;
                case 2:
                    indexYear = this.Years.IndexOf(this.Year);
                    if (indexYear >= 0 && indexYear < this.Years.Count - 1)
                    {
                        this.Year = this.Years[indexYear + 1];
                        OnYearChanged(null);
                    }
                    break;
                case 3:
                    indexMonth = this.Months.IndexOf(this.Month);
                    if (indexMonth > 0 && indexMonth < this.Months.Count)
                    {
                        this.Month = this.Months[indexMonth - 1];
                        OnMonthChanged(null);
                    }
                    break;
                case 5:
                    indexMonth = this.Months.IndexOf(this.Month);
                    if (indexMonth >= 0 && indexMonth < this.Months.Count - 1)
                    {
                        this.Month = this.Months[indexMonth + 1];
                        OnMonthChanged(null);
                    }
                    break;
                case 6:
                    indexDay = this.Days.IndexOf(this.Day);
                    if (indexDay > 0 && indexDay < this.Days.Count)
                    {
                        this.Day = this.Days[indexDay - 1];
                        OnDayChanged(null);
                    }
                    break;
                case 8:
                    indexDay = this.Days.IndexOf(this.Day);
                    if (indexDay >= 0 && indexDay < this.Days.Count - 1)
                    {
                        this.Day = this.Days[indexDay + 1];
                        OnDayChanged(null);
                    }
                    break;
            }
        }

        protected override void OnPaint(Graphics G)
        {
            base.OnPaint(G);
            StringFormat SF = new StringFormat();
            SF.Alignment = StringAlignment.Center;
            SF.LineAlignment = StringAlignment.Center;

            #region 年
            string strYear = this.Year.ToString() + "年";
            if (Years.Count == 0)
            {
                this.Year = 0;
                strYear = "无";
            }
            else if (this.Year == 0)
            {
                this.Year = this.Years[this.Years.Count - 1];
                OnYearChanged(null);
                strYear = this.Year.ToString() + "年";
            }
            G.DrawString("<", this.Parent.Font, Brushes.Green, this.rects[0], SF);
            G.DrawString(strYear, this.Parent.Font, Brushes.Red, this.rects[1], SF);
            G.DrawString(">", this.Parent.Font, Brushes.Green, this.rects[2], SF);
            #endregion

            #region 月
            string strMonth = this.Month.ToString() + "月";
            if (Months.Count == 0)
            {
                this.Month = 0;
                strMonth = "无";
            }
            else if (this.Month == 0)
            {
                this.Month = this.Months[this.Months.Count - 1];
                OnMonthChanged(null);
                strMonth = this.Month.ToString() + "月";
            }
            G.DrawString("<", this.Parent.Font, Brushes.Green, this.rects[3], SF);
            G.DrawString(strMonth, this.Parent.Font, Brushes.Red, this.rects[4], SF);
            G.DrawString(">", this.Parent.Font, Brushes.Green, this.rects[5], SF);
            #endregion

            #region 日
            string strDay = this.Day.ToString() + "日";
            if (Days.Count == 0)
            {
                this.Day = 0;
                strDay = "无";
            }
            else if (this.Day == 0)
            {
                this.Day = this.Days[this.Days.Count - 1];
                OnDayChanged(null);
                strDay = this.Day.ToString() + "日";
            }
            G.DrawString("<", this.Parent.Font, Brushes.Green, this.rects[6], SF);
            G.DrawString(strDay, this.Parent.Font, Brushes.Red, this.rects[7], SF);
            G.DrawString(">", this.Parent.Font, Brushes.Green, this.rects[8], SF);
            #endregion


        }
    }
}
